Functions in Odin
An overview of functions in Odin programming language. This post covers (1) function declaration; (2) parameters and results; (3) procedure overloading;
Table of Contents
1. Function Declaration
A function is called procedure in Odin. To define a function, use proc keyword.
func :: proc(x: int, y: int) -> int { /* ... */ }
Of course, parameters can have default values.
1.1. Multiple Parameters of Same Type
If consecutive parameters have same type, they can be compressed.
func :: proc(x, y, z: int) -> int { /* ... */ }
1.2. Variadic Parameters
1.3. Multiple Results
1.4. Named Results
1.5. Named Arguments
When calling a procedure, we can call with named arguments. The rule is that positional arguments must be in front of named arguments.
foo :: proc(value: int, name: string, x: bool, y: f32, z := 0) { }
foo(
134, // value
"hellope", // name
x=true,
y=4.5,
) // z = 0
2. Explicit Procedure Overloading
Suppose we have several functions, we can gather them explicitly using proc{ func1, func2 } to overload functions.
int_to_string :: proc(x: int) -> string {}
bool_to_string :: proc(x: bool) -> string {}
to_string :: proc{
int_to_string,
bool_to_string
}